package cn.lili.controller.recharge;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.ClientTypeEnum;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.kit.CashierSupport;
import cn.lili.modules.payment.entity.dto.CashierParam;
import cn.lili.modules.payment.entity.enums.PaymentClientEnum;
import cn.lili.modules.payment.entity.enums.PaymentMethodEnum;
import cn.lili.modules.payment.entity.vo.RechargeCashierParamVO;
import cn.lili.modules.wallet.handler.RechargeCashier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

/**
 * 交易收银台
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/4/14 16:39
 */
@RestController
@RequestMapping("/recharge/cashier")
@Api(tags = "余额充值收银台")
@RequiredArgsConstructor
@Validate
@Slf4j
public class RechargeCashierController {

    private final CashierSupport cashierSupport;

    private final RechargeCashier rechargeCashier;


    @PreventDuplicateSubmissions
    @ApiOperation(value = "线下收款")
    @ApiImplicitParam(name = "rechargeSn", value = "充值单号", required = true, dataType = "String", paramType = "path")
    @PostMapping(value = "/offline/{rechargeSn}")
    public ResultMessage<Object> offline(@PathVariable String rechargeSn) {

        if (!UserContext.getCurrentUser().getScene().equals(SceneEnums.MANAGER)) {
            throw new ServiceException("非管理员不能操作");
        }

        cashierSupport.generatePaymentForm(rechargeCashier.generatePaymentParams(rechargeSn, PaymentClientEnum.NATIVE,
                PaymentMethodEnum.BANK_TRANSFER));

        return ResultUtil.success();
    }

    @GetMapping(value = "/tradeDetail")
    @ApiOperation(value = "获取支付详情")
    public ResultMessage<Object> paymentParams(String sn, ClientTypeEnum clientType) {
        CashierParam cashierParam = cashierSupport.cashierParam(clientType.name());

        //过滤钱包支付
        cashierParam.setSupport(
                cashierParam.getSupport().stream().filter(support -> !support.equals(PaymentMethodEnum.WALLET.name())).toList()
        );

        RechargeCashierParamVO rechargeCashierParamVO = new RechargeCashierParamVO(cashierParam);
        return ResultUtil.data(rechargeCashier.handlerCashierParams(sn, rechargeCashierParamVO));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentMethod", value = "支付方式", paramType = "path", allowableValues = "WECHAT,ALIPAY"),
            @ApiImplicitParam(name = "paymentClient", value = "调起方式", paramType = "path", allowableValues = "APP,NATIVE,JSAPI,H5,MP")
    })
    @GetMapping(value = "/pay/{paymentMethodEnums}/{paymentClientEnums}")
    @ApiOperation(value = "支付")
    public ResultMessage<Object> payment(@PathVariable String paymentMethodEnums, @PathVariable String paymentClientEnums, String sn) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethodEnums);
        PaymentClientEnum paymentClientEnum = PaymentClientEnum.valueOf(paymentClientEnums);

        try {
            return cashierSupport.generatePaymentForm(rechargeCashier.generatePaymentParams(sn, paymentClientEnum, paymentMethodEnum));
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;
    }


}
